﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.Utils;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.LogTool;
using CastingWellEquipmentGUI.ViewModel.Common.WindowsManager;
using Confluent.Kafka;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace CastingWellEquipmentGUI.ViewModel.Pages
{
    /// <summary>
    /// 液压系统ViewModel
    /// </summary>
    public class HydraulicSystemPageViewModel : ViewModelBase
    {
        #region 变量/常量
        /// <summary>
        /// 当前窗体名称
        /// </summary>
        private const string showPageTitle = "HydraulicSystemPage";
        private string _CurrentControl;
        /// <summary>
        /// 当前选中的控件
        /// </summary>
        public string CurrentControl
        {
            get { return _CurrentControl; }
            set { _CurrentControl = value; RaisePropertyChanged("CurrentControl"); }
        }
        #endregion 变量/常量

        #region 页面属性
        #region Label显示
        /// <summary>
        /// PV1显示值
        /// </summary>
        public string Lable_OilTempPV
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_OilTempPV"); }
        }
        /// <summary>
        /// PV2显示值
        /// </summary>
        public string Lable_PV2
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_PV2"); }
        }
        /// <summary>
        /// 铸造速度PV1显示值
        /// </summary>
        public string Lable_CastingRateCV1
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_CastingRateCV1"); }
        }
        /// <summary>
        /// 铸造速度PV2显示值
        /// </summary>
        public string Lable_CastingRateCV2
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_CastingRateCV2"); }
        }
        /// <summary>
        /// 高速阀（上）PV
        /// </summary>
        public string Lable_PV7
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_PV7"); }
        }
        /// <summary>
        /// 高速阀（下）PV
        /// </summary>
        public string Lable_PV8
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_PV8"); }
        }
        /// <summary>
        /// PV9显示值
        /// </summary>
        public string Lable_PV9
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_PV9"); }
        }
        /// <summary>
        /// PV10显示值
        /// </summary>
        public string Lable_PV10
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_PV10"); }
        }
        #endregion Label显示

        #region Button显示
        private string _Button_CastingRateCV1;
        /// <summary>
        /// 铸造速度CV1设定值
        /// </summary>
        public string Button_CastingRateCV1
        {
            get { return _Button_CastingRateCV1; }
            set
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_CastingRateCV1", value))
                {
                    _Button_CastingRateCV1 = value;
                    RaisePropertyChanged("Button_CastingRateCV1");
                }
            }
        }
        private string _Button_CastingRateCV2;
        /// <summary>
        /// 铸造速度CV2设定值
        /// </summary>
        public string Button_CastingRateCV2
        {
            get { return _Button_CastingRateCV2; }
            set
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_CastingRateCV2", value))
                {
                    _Button_CastingRateCV2 = value;
                    RaisePropertyChanged("Button_CastingRateCV2");
                }
            }
        }
        private string _Button_CV1;
        /// <summary>
        /// 高速阀（上）CV
        /// </summary>
        public string Button_CV1
        {
            get { return _Button_CV1; }
            set
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_CV1", value))
                {
                    _Button_CV1 = value;
                    RaisePropertyChanged("Button_CV1");
                }
            }
        }
        private string _Button_CV2;
        /// <summary>
        /// 高速阀（下）CV
        /// </summary>
        public string Button_CV2
        {
            get { return _Button_CV2; }
            set
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_CV2", value))
                {
                    _Button_CV2 = value;
                    RaisePropertyChanged("Button_CV2");
                }
            }
        }
        private string _Button_CastLengthSP;
        /// <summary>
        /// 铸造长度SP设定
        /// </summary>
        public string Button_CastLengthSP
        {
            get { return _Button_CastLengthSP; }
            set
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_CastLengthSP", value))
                {
                    _Button_CastLengthSP = value;
                    RaisePropertyChanged("Button_CastLengthSP");
                }
            }
        }
        private string _Button_FurnaceDevSP;
        /// <summary>
        /// 炉倾翻偏移SP设定
        /// </summary>
        public string Button_FurnaceDevSP
        {
            get { return _Button_FurnaceDevSP; }
            set
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_FurnaceDevSP", value))
                {
                    _Button_FurnaceDevSP = value;
                    RaisePropertyChanged("Button_FurnaceDevSP");
                }
            }
        }
        private string _Button_FurnaceDevOF;
        /// <summary>
        /// 炉倾翻偏移OF设定
        /// </summary>
        public string Button_FurnaceDevOF
        {
            get { return _Button_FurnaceDevOF; }
            set
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_FurnaceDevOF", value))
                {
                    _Button_FurnaceDevOF = value;
                    RaisePropertyChanged("Button_FurnaceDevOF");
                }
            }
        }
        #endregion Button显示

        #region 阀门开关显示
        private string _Image_PressureDeviceUrl = "";
        /// <summary>
        /// 压力装置
        /// </summary>
        public string Image_PressureDeviceUrl
        {
            get { return _Image_PressureDeviceUrl; }
            set { _Image_PressureDeviceUrl = value; RaisePropertyChanged("Image_PressureDeviceUrl"); }
        }
        private string _Image_OilTapUpUrl = "";
        /// <summary>
        /// 液压油上阀
        /// </summary>
        public string Image_OilTapUpUrl
        {
            get { return _Image_OilTapUpUrl; }
            set { _Image_OilTapUpUrl = value; RaisePropertyChanged("Image_OilTapUpUrl"); }
        }
        private string _Image_OilTapMiddleUrl = "";
        /// <summary>
        /// 液压油中阀
        /// </summary>
        public string Image_OilTapMiddleUrl
        {
            get { return _Image_OilTapMiddleUrl; }
            set { _Image_OilTapMiddleUrl = value; RaisePropertyChanged("Image_OilTapMiddleUrl"); }
        }
        private string _Image_OilTapDownUrl = "";
        /// <summary>
        /// 液压油下阀
        /// </summary>
        public string Image_OilTapDownUrl
        {
            get { return _Image_OilTapDownUrl; }
            set { _Image_OilTapDownUrl = value; RaisePropertyChanged("Image_OilTapDownUrl"); }
        }
        private string _Image_PumpTap1Url = "";
        /// <summary>
        /// 循环泵阀
        /// </summary>
        public string Image_PumpTap1Url
        {
            get { return _Image_PumpTap1Url; }
            set { _Image_PumpTap1Url = value; RaisePropertyChanged("Image_PumpTap1Url"); }
        }
        private string _Image_PumpTap2Url = "";
        /// <summary>
        /// 1#主泵
        /// </summary>
        public string Image_PumpTap2Url
        {
            get { return _Image_PumpTap2Url; }
            set { _Image_PumpTap2Url = value; RaisePropertyChanged("Image_PumpTap2Url"); }
        }
        private string _Image_PumpTap3Url = "";
        /// <summary>
        /// 2#主泵
        /// </summary>
        public string Image_PumpTap3Url
        {
            get { return _Image_PumpTap3Url; }
            set { _Image_PumpTap3Url = value; RaisePropertyChanged("Image_PumpTap3Url"); }
        }
        private string _Image_PumpTap4Url = "";
        /// <summary>
        /// 先导泵
        /// </summary>
        public string Image_PumpTap4Url
        {
            get { return _Image_PumpTap4Url; }
            set { _Image_PumpTap4Url = value; RaisePropertyChanged("Image_PumpTap4Url"); }
        }
        #endregion 阀门开关显示

        #region 四通管道显示
        private string _Image_OnePipelineUpUrl = "";
        /// <summary>
        /// 四通1上
        /// </summary>
        public string Image_OnePipelineUpUrl
        {
            get { return _Image_OnePipelineUpUrl; }
            set { _Image_OnePipelineUpUrl = value; RaisePropertyChanged("Image_OnePipelineUpUrl"); }
        }
        private string _Image_OnePipelineDownUrl = "";
        /// <summary>
        /// 四通1下
        /// </summary>
        public string Image_OnePipelineDownUrl
        {
            get { return _Image_OnePipelineDownUrl; }
            set { _Image_OnePipelineDownUrl = value; RaisePropertyChanged("Image_OnePipelineDownUrl"); }
        }

        private string _Image_TwoPipelineUpUrl = "";
        /// <summary>
        /// 四通2上
        /// </summary>
        public string Image_TwoPipelineUpUrl
        {
            get { return _Image_TwoPipelineUpUrl; }
            set { _Image_TwoPipelineUpUrl = value; RaisePropertyChanged("Image_TwoPipelineUpUrl"); }
        }
        private string _Image_TwoPipelineDownUrl = "";
        /// <summary>
        /// 四通2下
        /// </summary>
        public string Image_TwoPipelineDownUrl
        {
            get { return _Image_TwoPipelineDownUrl; }
            set { _Image_TwoPipelineDownUrl = value; RaisePropertyChanged("Image_TwoPipelineDownUrl"); }
        }
        #endregion 四通管道显示

        #region 高速和低速阀
        private string _Button_HighSpeedTapImageUrl = "";
        /// <summary>
        /// 高速阀图片路径
        /// </summary>
        public string Button_HighSpeedTapImageUrl
        {
            get { return _Button_HighSpeedTapImageUrl; }
            set { _Button_HighSpeedTapImageUrl = value; RaisePropertyChanged("Button_HighSpeedTapImageUrl"); }
        }
        private string _Button_LowSpeedTapImageUrl = "";
        /// <summary>
        /// 低速阀图片路径
        /// </summary>
        public string Button_LowSpeedTapImageUrl
        {
            get { return _Button_LowSpeedTapImageUrl; }
            set { _Button_LowSpeedTapImageUrl = value; RaisePropertyChanged("Button_LowSpeedTapImageUrl"); }
        }
        #endregion

        #region 液压油罐液位显示
        private string _Image_OilTankLevelUrl = "";
        /// <summary>
        /// 液压油罐液位
        /// </summary>
        public string Image_OilTankLevelUrl
        {
            get { return _Image_OilTankLevelUrl; }
            set { _Image_OilTankLevelUrl = value; RaisePropertyChanged("Image_OilTankLevelUrl"); }
        }
        private string _Image_HHUrl = "";
        /// <summary>
        /// 高高位
        /// </summary>
        public string Image_HHUrl
        {
            get { return _Image_HHUrl; }
            set { _Image_HHUrl = value; RaisePropertyChanged("Image_HHUrl"); }
        }
        private string _Image_HUrl = "";
        /// <summary>
        /// 高位
        /// </summary>
        public string Image_HUrl
        {
            get { return _Image_HUrl; }
            set { _Image_HUrl = value; RaisePropertyChanged("Image_HUrl"); }
        }
        private string _Image_LUrl = "";
        /// <summary>
        /// 低位
        /// </summary>
        public string Image_LUrl
        {
            get { return _Image_LUrl; }
            set { _Image_LUrl = value; RaisePropertyChanged("Image_LUrl"); }
        }
        private string _Image_LLUrl = "";
        /// <summary>
        /// 低低位
        /// </summary>
        public string Image_LLUrl
        {
            get { return _Image_LLUrl; }
            set { _Image_LLUrl = value; RaisePropertyChanged("Image_LLUrl"); }
        }
        #endregion 液压油罐液位显示

        #region 预检测项目
        private Visibility _PreDetectionVisibility;
        /// <summary>
        /// 预检测状态
        /// 注：控制预检测框显隐
        /// </summary>
        public Visibility PreDetectionVisibility
        {
            get { return _PreDetectionVisibility; }
            set { _PreDetectionVisibility = value; RaisePropertyChanged("PreDetectionVisibility"); }
        }
        /// <summary>
        /// 铸造比例阀在正确开度
        /// </summary>
        public Brush Label_PreCheck1
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck1") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 铸造速度为零
        /// </summary>
        public Brush Label_PreCheck2
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck2") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 紧急铸造支路正确阀位
        /// </summary>
        public Brush Label_PreCheck3
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck3") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 铸造支路正确阀位
        /// </summary>
        public Brush Label_PreCheck4
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck4") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 液压比例阀已标定
        /// </summary>
        public Brush Label_PreCheck5
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck5") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 铸造循环阀打开
        /// </summary>
        public Brush Label_PreCheck6
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck6") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 液压过滤器正常
        /// </summary>
        public Brush Label_PreCheck7
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck7") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 紧急铸造开始阀关闭
        /// </summary>
        public Brush Label_PreCheck8
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck8") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 紧急铸造停止阀打开
        /// </summary>
        public Brush Label_PreCheck9
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck9") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 液压电器柜温度正常
        /// </summary>
        public Brush Label_PreCheck10
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck10") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 顶板位置开关电源正常
        /// </summary>
        public Brush Label_PreCheck11
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck11") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 顶板位于顶部位置
        /// </summary>
        public Brush Label_PreCheck12
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck12") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 顶板位于铸造位置
        /// </summary>
        public Brush Label_PreCheck13
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck13") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 铸造长度等于零
        /// </summary>
        public Brush Label_PreCheck14
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck14") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        #endregion 预检测项目

        #endregion 页面属性

        #region 定义页面执行命令
        /// <summary>
        /// 按钮命令
        /// </summary>
        public DelegateCommand ShowModalPanelCommand { get; set; }
        /// <summary>
        /// 高速和低速阀命令
        /// </summary>
        public DelegateCommand SpeedTapCommand { get; set; }
        /// <summary>
        /// 质量流量计设置命令
        /// </summary>
        public DelegateCommand ShowFlowmeterSetPanelCommand { get; set; }
        /// <summary>
        /// 质量流量计(递增或递减)命令
        /// </summary>
        public DelegateCommand SetSelectControlTagValueByCalculationCommand { get; set; }
        /// <summary>
        /// 隐藏手动自动切换窗体命令
        /// </summary>
        public DelegateCommand HideFeederPageSettingPanel1WindowCommand { get; set; }
        #endregion 定义页面执行命令

        #region 线程对象
        /// <summary>
        /// 标签值变化监听线程
        /// </summary>
        private Thread tagInfoSyncThread = null;
        #endregion 线程对象

        #region 方法
        /// <summary>
        /// 构造函数
        /// </summary>
        public HydraulicSystemPageViewModel()
        {
            // 初始化命令
            initCommand();
            // 初始化线程
            initThread();
            // 消息通道注册
            initMessenger();
            // 初始化基础信息
            initBaseSet();
        }

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void initCommand()
        {
            /**
             * 按钮命令
             * **/
            ShowModalPanelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(showModalPanelHandler)
            };
            // 高速和低速阀命令
            SpeedTapCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(speedTapCommandHandler)
            };
            // 质量流量计设置命令
            ShowFlowmeterSetPanelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(showFlowmeterSetPanelCommandHandler)
            };
            // 质量流量计(递增或递减)命令
            SetSelectControlTagValueByCalculationCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(setSelectControlTagValueByCalculationHandler)
            };
            // 关闭质量流量计设置弹框
            HideFeederPageSettingPanel1WindowCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(hidePageSettingPanel1Handler)
            };
        }

        /// <summary>
        /// 隐藏命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void hidePageSettingPanel1Handler(object obj)
        {
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "FlowmeterSetPanelMessage"));
        }

        /// <summary>
        /// 质量流量计设置命令
        /// </summary>
        /// <param name="obj"></param>
        private void showFlowmeterSetPanelCommandHandler(object obj)
        {
            CurrentControl = obj.ToString();
            WindowManager.ShowDialog("FlowmeterSetPanel", null);
        }

        /// <summary>
        /// 高速和低速阀命令
        /// </summary>
        /// <param name="obj"></param>
        private void speedTapCommandHandler(object obj)
        {
            // 获取参数，此处参数为按钮名称
            string param = obj.ToString();
            WindowManager.ShowModalOnOrOffPanel("OnOrOffPanel", showPageTitle + param);
        }

        /// <summary>
        /// 按钮弹框
        /// </summary>
        /// <param name="obj"></param>
        private void showModalPanelHandler(object obj)
        {
            string param = obj.ToString();
            switch (param)
            {
                // 铸造速度CV1设定值
                case "Button_CastingRateCV1":
                    WindowManager.ShowModalDigitalInputPanel("SetCVPanel", showPageTitle + "Button_CastingRateCV1", Button_CastingRateCV1);
                    break;
                // 铸造速度CV2设定值
                case "Button_CastingRateCV2":
                    WindowManager.ShowModalDigitalInputPanel("SetCVPanel", showPageTitle + "Button_CastingRateCV2", Button_CastingRateCV2);
                    break;
                // 高速阀（上）CV
                case "Button_CV1":
                    WindowManager.ShowModalDigitalInputPanel("SetCVPanel", showPageTitle + "Button_CV1", Button_CV1);
                    break;
                // 高速阀（下）CV
                case "Button_CV2":
                    WindowManager.ShowModalDigitalInputPanel("SetCVPanel", showPageTitle + "Button_CV2", Button_CV2);
                    break;
                // 铸造长度SP设定
                case "Button_CastLengthSP":
                    WindowManager.ShowModalDigitalInputPanel("SetCVPanel", showPageTitle + "Button_CastLengthSP", Button_CastLengthSP);
                    break;
                // 炉倾翻偏移SP设定
                case "Button_FurnaceDevSP":
                    WindowManager.ShowModalDigitalInputPanel("SetCVPanel", showPageTitle + "Button_FurnaceDevSP", Button_FurnaceDevSP);
                    break;
                // 炉倾翻偏移OF设定
                case "Button_FurnaceDevOF":
                    WindowManager.ShowModalDigitalInputPanel("SetCVPanel", showPageTitle + "Button_FurnaceDevOF", Button_FurnaceDevOF);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 初始化线程
        /// </summary>
        private void initThread()
        {
            // 标签值变化监听线程
            tagInfoSyncThread = new Thread(syncThreadFunc);
            tagInfoSyncThread.SetApartmentState(ApartmentState.STA);
            tagInfoSyncThread.IsBackground = true;
            tagInfoSyncThread.Start();
        }

        /// <summary>
        /// 标签值变化监听
        /// </summary>
        private void syncThreadFunc()
        {
            // 创建消费者对象
            IConsumer<string, object> KafkaConsumer = KafkaHelper.GetConsumer();
            while (true)
            {
                if (SystemConfig.systemState == "预检测" || SystemConfig.systemState == "可见许可")
                {
                    PreDetectionVisibility = Visibility.Visible;
                }
                else
                {
                    PreDetectionVisibility = Visibility.Hidden;
                }
                // 网络中断不进行消费监听
                if (SystemConfig.SystemStatus == "网络中断")
                {
                    Thread.Sleep(1000);
                    continue;
                }
                if (ThreadController.RoundBarHydraulicSystemThreadState)
                {
                    //开始消费
                    var result = KafkaConsumer.Consume();
                    if (result != null)
                    {
                        //手动提交
                        KafkaConsumer.Commit(result);
                        //获取消息内容
                        string tagId = result.Message.Value.ToString();
                        HashSet<Control> hashSet = KafkaHelper.UpdateCurrentUserTagValue(Int32.Parse(tagId), KafkaHelper.GetTagValueByKey(tagId.ToString()), showPageTitle);
                        if (hashSet != null && hashSet.Count > 0)
                        {
                            DoChanged(hashSet);
                        }
                    }
                }
                Thread.Sleep(10);
            }
        }

        /// <summary>
        /// 执行动作
        /// </summary>
        /// <param name="hashSet"></param>
        private void DoChanged(HashSet<Control> hashSet)
        {

            //遍历返回的控件信息刷新对应的控件
            foreach (Control item in hashSet)
            {
                switch (item.ControlName)
                {
                    /** 1、显示页面阀门的开关 **/
                    // 液压油液位
                    case "Image_OilTankLevel":
                        string value = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName);
                        if (value == "1")
                        {
                            // 高高位
                            Image_OilTankLevelUrl = "/images/HydraulicSystem/HydraulicTank_HH.png";
                            Image_HHUrl = "/images/HydraulicSystem/HH_A.png";
                            Image_HUrl = "/images/HydraulicSystem/H.png";
                            Image_LUrl = "/images/HydraulicSystem/L.png";
                            Image_LLUrl = "/images/HydraulicSystem/LL.png";
                        }
                        else if (value == "2")
                        {
                            // 高位
                            Image_OilTankLevelUrl = "/images/HydraulicSystem/HydraulicTank_H.png";
                            Image_HHUrl = "/images/HydraulicSystem/HH.png";
                            Image_HUrl = "/images/HydraulicSystem/H_A.png";
                            Image_LUrl = "/images/HydraulicSystem/L.png";
                            Image_LLUrl = "/images/HydraulicSystem/LL.png";
                        }
                        else if (value == "3")
                        {
                            // 低位
                            Image_OilTankLevelUrl = "/images/HydraulicSystem/HydraulicTank_L.png";
                            Image_HHUrl = "/images/HydraulicSystem/HH.png";
                            Image_HUrl = "/images/HydraulicSystem/H.png";
                            Image_LUrl = "/images/HydraulicSystem/L_A.png";
                            Image_LLUrl = "/images/HydraulicSystem/LL.png";
                        }
                        else if (value == "4")
                        {
                            // 低低位
                            Image_OilTankLevelUrl = "/images/HydraulicSystem/HydraulicTank_LL.png";
                            Image_HHUrl = "/images/HydraulicSystem/HH.png";
                            Image_HUrl = "/images/HydraulicSystem/H.png";
                            Image_LUrl = "/images/HydraulicSystem/L.png";
                            Image_LLUrl = "/images/HydraulicSystem/LL_A.png";
                        }
                        else
                        {
                            // 液位正常
                            Image_OilTankLevelUrl = "/images/HydraulicSystem/HydraulicTank_N.png";
                            Image_HHUrl = "/images/HydraulicSystem/HH.png";
                            Image_HUrl = "/images/HydraulicSystem/H.png";
                            Image_LUrl = "/images/HydraulicSystem/L.png";
                            Image_LLUrl = "/images/HydraulicSystem/LL.png";
                        }
                        break;
                    /** 高速阀开启 **/
                    case "Button_HighSpeedTap_Open":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) == "true")
                        {
                            Button_HighSpeedTapImageUrl = "/images/HydraulicSystem/SpeedValveOpen.png";
                        }
                        break;
                    /** 高速阀关闭 **/
                    case "Button_HighSpeedTap_Close":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) == "true")
                        {
                            Button_HighSpeedTapImageUrl = "/images/HydraulicSystem/SpeedValveClose.png";
                        }
                        break;
                    /** 低速阀开启 **/
                    case "Button_LowSpeedTap_Open":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) == "true")
                        {
                            Button_LowSpeedTapImageUrl = "/images/HydraulicSystem/SpeedValveOpen.png";
                        }
                        break;
                    /** 低速阀关闭 **/
                    case "Button_LowSpeedTap_Close":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) == "true")
                        {
                            Button_LowSpeedTapImageUrl = "/images/HydraulicSystem/SpeedValveClose.png";
                        }
                        break;
                    // 液压油上阀
                    case "Image_OilTapUp":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_OilTapUpUrl = "/images/HydraulicSystem/UnobstructedValve.png";
                        }
                        else
                        {
                            Image_OilTapUpUrl = "/images/HydraulicSystem/UnobstructedValveClose.png";
                        }
                        break;
                    // 压力装置
                    case "Image_PressureDevice":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_PressureDeviceUrl = "/images/HydraulicSystem/Device.png";
                        }
                        else
                        {
                            Image_PressureDeviceUrl = "/images/HydraulicSystem/DeviceClose.png";
                        }
                        break;
                    // 液压油中阀
                    case "Image_OilTapMiddle":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_OilTapMiddleUrl = "/images/HydraulicSystem/UnobstructedValve.png";
                        }
                        else
                        {
                            Image_OilTapMiddleUrl = "/images/HydraulicSystem/UnobstructedValveClose.png";
                        }
                        break;
                    // 液压油下阀
                    case "Image_OilTapDown":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_OilTapDownUrl = "/images/HydraulicSystem/UnobstructedValve.png";
                        }
                        else
                        {
                            Image_OilTapDownUrl = "/images/HydraulicSystem/UnobstructedValveClose.png";
                        }
                        break;
                    // 循环泵阀
                    case "Image_PumpTap1":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_PumpTap1Url = "/images/HydraulicSystem/UnobstructedValve.png";
                        }
                        else
                        {
                            Image_PumpTap1Url = "/images/HydraulicSystem/UnobstructedValveClose.png";
                        }
                        break;
                    // 1#主泵
                    case "Image_PumpTap2":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_PumpTap2Url = "/images/HydraulicSystem/UnobstructedValve.png";
                        }
                        else
                        {
                            Image_PumpTap2Url = "/images/HydraulicSystem/UnobstructedValveClose.png";
                        }
                        break;
                    // 2#主泵
                    case "Image_PumpTap3":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_PumpTap3Url = "/images/HydraulicSystem/UnobstructedValve.png";
                        }
                        else
                        {
                            Image_PumpTap3Url = "/images/HydraulicSystem/UnobstructedValveClose.png";
                        }
                        break;
                    // 先导泵
                    case "Image_PumpTap4":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_PumpTap4Url = "/images/HydraulicSystem/UnobstructedValve.png";
                        }
                        else
                        {
                            Image_PumpTap4Url = "/images/HydraulicSystem/UnobstructedValveClose.png";
                        }
                        break;
                    /** 2、四通管道 **/
                    // 四通1上
                    case "Image_OnePipelineUp":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_OnePipelineUpUrl = "/images/HydraulicSystem/PipelineUpOpen.png";
                        }
                        else
                        {
                            Image_OnePipelineUpUrl = "/images/HydraulicSystem/PipelineUpClose.png";
                        }
                        break;
                    // 四通1下
                    case "Image_OnePipelineDown":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_OnePipelineDownUrl = "/images/HydraulicSystem/PipelineDownOpen.png";
                        }
                        else
                        {
                            Image_OnePipelineDownUrl = "/images/HydraulicSystem/PipelineDownClose.png";
                        }
                        break;
                    // 四通2上
                    case "Image_TwoPipelineUp":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_TwoPipelineUpUrl = "/images/HydraulicSystem/PipelineUpOpen.png";
                        }
                        else
                        {
                            Image_TwoPipelineUpUrl = "/images/HydraulicSystem/PipelineUpClose.png";
                        }
                        break;
                    // 四通2下
                    case "Image_TwoPipelineDown":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) != "false")
                        {
                            Image_TwoPipelineDownUrl = "/images/HydraulicSystem/PipelineDownOpen.png";
                        }
                        else
                        {
                            Image_TwoPipelineDownUrl = "/images/HydraulicSystem/PipelineDownClose.png";
                        }
                        break;
                    default:
                        /** 3、刷新页面值变化信息 **/
                        RaisePropertyChanged(item.ControlName);
                        break;
                }
            }
        }

        /// <summary>
        /// 消息通道注册
        /// </summary>
        private void initMessenger()
        {
            // 铸造速度CV1设定值
            Messenger.Default.Register<string>(this, showPageTitle + "Button_CastingRateCV1", (message) =>
            {
                Button_CastingRateCV1 = message.ToString(); ;
            });
            // 铸造速度CV2设定值
            Messenger.Default.Register<string>(this, showPageTitle + "Button_CastingRateCV2", (message) =>
            {
                Button_CastingRateCV2 = message.ToString(); ;
            });
            // 高速阀（上）CV
            Messenger.Default.Register<string>(this, showPageTitle + "Button_CV1", (message) =>
            {
                Button_CV1 = message.ToString(); ;
            });
            // 高速阀（下）CV
            Messenger.Default.Register<string>(this, showPageTitle + "Button_CV2", (message) =>
            {
                Button_CV2 = message.ToString(); ;
            });
            // 气铸造长度SP设定
            Messenger.Default.Register<string>(this, showPageTitle + "Button_CastLengthSP", (message) =>
            {
                Button_CastLengthSP = message.ToString(); ;
            });
            // 炉倾翻偏移SP设定
            Messenger.Default.Register<string>(this, showPageTitle + "Button_FurnaceDevSP", (message) =>
            {
                Button_FurnaceDevSP = message.ToString(); ;
            });
            // 炉倾翻偏移OF设定
            Messenger.Default.Register<string>(this, showPageTitle + "Button_FurnaceDevOF", (message) =>
            {
                Button_FurnaceDevOF = message.ToString(); ;
            });
            // 高速阀开启或关闭
            Messenger.Default.Register<string>(this, showPageTitle + "Button_HighSpeedTap", (message) =>
            {
                setHighSpeedTapOnOrOff(message.ToString());
            });
            // 低速阀开启或关闭
            Messenger.Default.Register<string>(this, showPageTitle + "Button_LowSpeedTap", (message) =>
            {
                setLowSpeedTapOnOrOff(message.ToString());
            });
            //页面切换执行控件刷新
            Messenger.Default.Register<string>(this, showPageTitle + "_DoChange", (message) =>
            {
                HashSet<Control> tempHashSet = SysCommon.getCurrentControl(showPageTitle);
                if (tempHashSet.Count > 0)
                {
                    DoChanged(tempHashSet);
                }
            });
        }

        /// <summary>
        /// 高速阀开启或关闭
        /// </summary>
        /// <param name="message">True Or False</param>
        private void setHighSpeedTapOnOrOff(string message)
        {
            if (message == "" || message == null)
            {
                return;
            }
            if (message == "True")
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_HighSpeedTap_Open", "true"))
                {
                    Button_HighSpeedTapImageUrl = "/images/HydraulicSystem/SpeedValveOpen.png";
                }
            }
            else if (message == "False")
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_HighSpeedTap_Close", "true"))
                {
                    Button_HighSpeedTapImageUrl = "/images/HydraulicSystem/SpeedValveClose.png";
                }
            }
        }

        /// <summary>
        /// 低速阀开启或关闭
        /// </summary>
        /// <param name="message">True Or False</param>
        private void setLowSpeedTapOnOrOff(string message)
        {
            if (message == "" || message == null)
            {
                return;
            }
            if (message == "True")
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_LowSpeedTap_Open", "true"))
                {
                    Button_LowSpeedTapImageUrl = "/images/HydraulicSystem/SpeedValveOpen.png";
                }
            }
            else if (message == "False")
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_LowSpeedTap_Close", "true"))
                {
                    Button_LowSpeedTapImageUrl = "/images/HydraulicSystem/SpeedValveClose.png";
                }
            }
        }

        /// <summary>
        /// 初始化基础信息
        /// </summary>
        private void initBaseSet()
        {
            // 页面初始化
            HashSet<Control> tempHashSet = SysCommon.getCurrentControl(showPageTitle);
            if (tempHashSet.Count > 0)
            {
                DoChanged(tempHashSet);
            }
            /** 设定值初始化 **/
            _Button_CastingRateCV1 = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_CastingRateCV1");
            RaisePropertyChanged("Button_CastingRateCV1");
            _Button_CastingRateCV2 = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_CastingRateCV2");
            RaisePropertyChanged("Button_CastingRateCV2");

            _Button_CV1 = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_CV1");
            RaisePropertyChanged("Button_CV1");
            _Button_CV2 = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_CV2");
            RaisePropertyChanged("Button_CV2");

            _Button_CastLengthSP = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_CastLengthSP");
            RaisePropertyChanged("Button_CastLengthSP");
            _Button_FurnaceDevSP = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_FurnaceDevSP");
            RaisePropertyChanged("Button_FurnaceDevSP");
            _Button_FurnaceDevOF = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_FurnaceDevOF");
            RaisePropertyChanged("Button_FurnaceDevOF");
        }

        /// <summary>
        /// 质量流量计(递增或递减)命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void setSelectControlTagValueByCalculationHandler(object obj)
        {
            // 点击增加或减少按钮(+1,+2,-1,-2)
            string para = obj.ToString();
            // 设定标签信息
            BusinessCmd setTagValueMsg = new BusinessCmd();
            setTagValueMsg.BusinessCmdCode = 3;
            setTagValueMsg.Guid = Guid.NewGuid().ToString();
            if (CurrentControl == "Button_FlowmeterOne")
            {
                // 质量流量计1按钮
                if (para == "+1")
                {
                    setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterOnePlus1");
                    setTagValueMsg.TagValue = "1";
                }
                else if (para == "+2")
                {
                    setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterOnePlus2");
                    setTagValueMsg.TagValue = "2";
                }
                else if (para == "-1")
                {
                    setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterOneSubtract1");
                    setTagValueMsg.TagValue = "-1";
                }
                else if (para == "-2")
                {
                    setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterOneSubtract2");
                    setTagValueMsg.TagValue = "-2";
                }
            }
            else if (CurrentControl == "Button_FlowmeterTwo")
            {
                // 质量流量计2按钮
                if (para == "+1")
                {
                    setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterTwoPlus1");
                    setTagValueMsg.TagValue = "1";
                }
                else if (para == "+2")
                {
                    setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterTwoPlus2");
                    setTagValueMsg.TagValue = "2";
                }
                else if (para == "-1")
                {
                    setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterTwoSubtract1");
                    setTagValueMsg.TagValue = "-1";
                }
                else if (para == "-2")
                {
                    setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterTwoSubtract2");
                    setTagValueMsg.TagValue = "-2";
                }
            }
            // 发送消息
            if (setTagValueMsg.TagValue != null)
            {
                KafkaHelper.SendGatewayBusinessCmd(setTagValueMsg);
            }
        }

        #endregion 方法

    }
}
